<?php
/**
 * Created By base
 * Author: Virus
 * Date: 2020/3/26
 * Time: 22:02
 */

namespace app\admin\controller;


use app\common\logic\CategoryLogic;
use app\common\lib\Tree;
use think\App;
use think\Request;

class Category extends Base
{
    protected $logic = null;
    protected $categoryList = [];

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->logic = new CategoryLogic();

        $tree               = Tree::instance();
        $this->categoryList = $tree->init($this->logic->getCategoryLists(), 'pid');
        $this->categoryList = $tree->getTreeList($tree->getTreeArray(0), 'title');
    }


    /**
     * 分类列表
     * @return \think\response\View
     */
    public function index()
    {
        $list = $this->categoryList;

        return view('category/index', compact('list'));
    }

    /**
     * 添加分类
     * @return \think\response\View
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = input('post.');
            try {
                $res = $this->logic->addCategory($data);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            if ($res) {
                $this->success($data['title']."分类添加成功", url('Category/index'));
            }
            $this->error('分类添加失败');
        } else {
            $pid        = input("param.pid", 0, "intval");
            $categories = $this->categoryList;

            return view('category/add', compact('categories', 'pid'));
        }
    }

    /**
     * 编辑分类
     * @return \think\response\View
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $id         = input("post.id");
            $updateData = [
                'pid'    => input('post.pid', 0, 'intval'),
                'title'  => input("post.title", "", "trim"),
                'order'  => input('post.order', 0, 'intval'),
                'status' => input('post.status', 0, 'intval'),
            ];
            try {
                $res = $this->logic->updateCategory($id, $updateData);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }

            if ($res) {
                $this->success("分类编辑成功", url('Category/index'));
            }
            $this->error("分类编辑失败");
        } else {
            $categories   = $this->categoryList;
            $id           = input("param.id", 0, "intval");
            $category_one = $this->logic->getCategoryOneById($id);

            return view('category/edit', compact('categories', 'category_one'));
        }
    }

    /**
     * 删除分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        if (!$request->isPost() || !$request->isAjax()) {
            return writeJson(0, "请求类型不合法");
        }

        $id = $request->post('id', 0, 'intval');
        try {
            $result = $this->logic->deleteById($id);
        } catch (\Exception $e) {
            return writeJson(0, $e->getMessage());
        }

        if ($result) {
            return writeJson(1, "分类删除成功");
        }

        return writeJson(0, "分类删除失败");
    }
}